/**
 * 事件管理器 - 负责弹幕事件的触发和管理
 */
import type { BarrageEvents, BarrageItem } from '../../types'

/**
 * 事件管理器类
 */
export class EventManager {
  private events: BarrageEvents

  constructor(events: BarrageEvents = {}) {
    this.events = events
  }

  /**
   * 更新事件回调
   */
  updateEvents(newEvents: BarrageEvents): void {
    this.events = { ...this.events, ...newEvents }
  }

  /**
   * 获取所有事件
   */
  getEvents(): BarrageEvents {
    return this.events
  }

  /**
   * 触发弹幕添加事件
   */
  onAdd(barrage: BarrageItem): void {
    this.events.onAdd?.(barrage)
  }

  /**
   * 触发弹幕移除事件
   */
  onRemove(barrage: BarrageItem): void {
    this.events.onRemove?.(barrage)
  }

  /**
   * 触发容量变化事件
   */
  onCapacityChange(activeCount: number, maxCapacity: number): void {
    this.events.onCapacityChange?.(activeCount, maxCapacity)
  }

  /**
   * 触发点击事件
   */
  onClick(barrage: BarrageItem, event: MouseEvent): void {
    this.events.onClick?.(barrage, event)
  }

  /**
   * 触发悬停事件
   */
  onHover(barrage: BarrageItem, event: MouseEvent): void {
    this.events.onHover?.(barrage, event)
  }

  /**
   * 检查某个事件是否有监听器
   */
  hasListener(eventName: keyof BarrageEvents): boolean {
    return this.events[eventName] !== undefined
  }

  /**
   * 移除所有事件监听器
   */
  removeAllListeners(): void {
    this.events = {}
  }
}

